
package cn.lym.zxxx.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class SerializeUtil {


    public static byte[] serializeObject(Object object) {
        try (ByteArrayOutputStream saos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(saos)) {
            oos.writeObject(object);
            oos.flush();
            return saos.toByteArray();
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }


    public static Object deserializeObject(byte[] buf) {
        if (buf == null) {
            return null;
        }
        try (ByteArrayInputStream sais = new ByteArrayInputStream(buf); ObjectInputStream ois = new ObjectInputStream(sais)) {
            Object object = ois.readObject();
            return object;
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }
}
